<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\ChOperationLog;
use App\CustHub\Annotation\ChPermission;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Dto\AddFriendDailyDataDto;
use App\CustHub\Dto\AddFriendGroupDataDto;
use App\CustHub\Dto\AddFriendRecordDto;
use App\CustHub\Dto\AddFriendRobotDataDto;
use App\CustHub\Mapper\ChUserMapper;
use App\CustHub\Model\ChAddFriendConfig;
use App\CustHub\Request\ActiveAddFriendConfigRequest;
use App\CustHub\Request\ActiveAddFriendRecordRequest;
use App\CustHub\Request\ActiveAddFriendRobotRequest;
use App\CustHub\Service\ActiveAddFriendConfigService;
use App\CustHub\Service\ActiveAddFriendContentService;
use App\CustHub\Service\ActiveAddFriendGroupService;
use App\CustHub\Service\ActiveAddFriendRecordService;
use App\CustHub\Service\ActiveAddFriendRobotService;
use App\CustHub\Service\ActiveAddFriendRoomService;
use App\CustHub\Service\ActiveAddFriendService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Mine\Exception\NormalStatusException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;


#[Controller(prefix: 'ch/attract/add'),ApiAuth,VerifySignature]
class ActiveAddFriendController extends BaseController
{
    #[Inject]
    protected ActiveAddFriendService $service;

    #[Inject]
    protected ActiveAddFriendConfigService $configService;

    #[Inject]
    protected ActiveAddFriendRoomService $roomService;

    #[Inject]
    protected ChAddFriendConfig $config;

    #[Inject]
    protected ActiveAddFriendContentService $contentService;

    #[Inject]
    protected ActiveAddFriendRecordService $recordService;

    #[Inject]
    protected ActiveAddFriendRobotService $robotService;

    #[Inject]
    protected ActiveAddFriendGroupService $groupService;

    public array $noNeedLogin = ['exportRecord','exportDailyData','exportRobotData','exportGroupData'];
    public array $noNeedVerifySign = ['exportRecord','exportDailyData','exportRobotData','exportGroupData'];

    /**
     * 获取分组列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('groupList'), ChPermission("ch:attract:add:groupList")]
    public function groupList(): ResponseInterface
    {
        return $this->success($this->groupService->groupList($this->request->all()));
    }

    /**
     * 新建分组
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('saveGroup'), ChPermission("ch:attract:add:saveGroup")]
    public function saveGroup(): ResponseInterface
    {
        $params = $this->request->all();
        return $this->success(['id' => $this->groupService->saveGroup($params)]);
    }

    /**
     * 修改分组名称
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PutMapping('updateGroup'), ChPermission("ch:attract:add:updateGroup")]
    public function updateGroup(): ResponseInterface
    {
        $params = $this->request->all();
        return $this->groupService->updateGroup($params) ? $this->success() : $this->error() ;
    }

    /**
     * 删除分组名称
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[DeleteMapping('deleteGroup'), ChPermission("ch:attract:add:deleteGroup")]
    public function deleteGroup(): ResponseInterface
    {
        $id = (int)$this->request->input('id');
        return $this->groupService->delete([$id]) ? $this->success() : $this->error() ;
    }

    /**
     * 获取配置
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PostMapping('config'), ChPermission("ch:attract:add:config")]
    public function getConfig(): ResponseInterface
    {
        $params = $this->request->all();
        return $this->success($this->configService->getConfig($params));
    }

    /**
     * 获取运营配置
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PostMapping('operateConfig'), ChPermission("ch:attract:add:operateConfig")]
    public function getOperateConfig(): ResponseInterface
    {
        return $this->success($this->configService->getOperateConfig($this->request->all()));
    }

    /**
     * 保存配置
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PostMapping('saveConfig'), ChPermission("ch:attract:add:saveConfig")]
    public function saveConfig(ActiveAddFriendConfigRequest $request)
    {
        $data = $request->all();
        return $this->configService->saveConfig($data) ? $this->success() : $this->error();
    }

    /**
     * 保存运营配置
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PostMapping('saveOperateConfig'),ChPermission("ch:attract:add:saveOperateConfig"),ChOperationLog]
    public function saveOperateConfig()
    {
        $data = $this->request->all();
        $uid = $data['uid'];
        //$beforeData =  $this->roomService->getRoomWxIdsAndMaxNumber($uid);
        //$this->configService->saveOperateConfig($uid, $data) ;
        return $this->roomService->updateRoom( $data)? $this->success() : $this->error();
    }

    public function addCustomRoom()
    {

    }

    /**
     * 获取话术列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PostMapping('contentList'), ChPermission('ch:attract:add:contentList')]
    public function contentList(): ResponseInterface
    {
        return $this->success($this->contentService->contentList($this->request->all()));
    }

    /**
     * 添加话术
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PostMapping('saveContent'),ChPermission('ch:attract:add:saveContent'),ChOperationLog]
    public function save(): ResponseInterface
    {
        $params = $this->request->all();
        return $this->contentService->save($params)?$this->success():$this->error();
    }

    /**
     * 更改话术状态
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PutMapping('changeContentStatus'),ChPermission('ch:attract:add:changeContentStatus'),ChOperationLog]
    public function changeStatus():ResponseInterface
    {
        $data = $this->request->all();
        $id = $data['id'];
        $value = $data['value'];
        return $this->contentService->changeStatus($id,$value)?$this->success():$this->error();
    }

    /**
     * 编辑话术
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PutMapping('updateContent'),ChPermission('ch:attract:add:updateContent'),ChOperationLog]
    public function update():ResponseInterface
    {
        return $this->contentService->update($this->request->input('id'),$this->request->all())?$this->success():$this->error();
    }

    /**
     * 删除话术
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[DeleteMapping('deleteContent'), ChPermission('ch:attract:add:deleteContent'), ChOperationLog]
    public function delete(): ResponseInterface
    {
        return $this->contentService->realDelete($this->request->all()) ? $this->success() : $this->error();
    }

    /**
     * 获取记录列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PostMapping('recordList'),ChPermission('ch:attract:add:recordList')]
    public function recordList()
    {
        return $this->success($this->recordService->recordList($this->request->all()));
    }

    /**
     * 任务中心列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PostMapping('taskList'),ChPermission('ch:attract:record:taskList')]
    public function taskList()
    {
        return $this->success($this->recordService->taskList($this->request->all()));
    }


    /**
     * 新增记录
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PostMapping('saveRecord'), ChPermission('ch:attract:saveRecord'), ChOperationLog]
    public function saveRecord()
    {
        $params = $this->request->all();
        return $this->recordService->saveRecord($params) ? $this->success() : $this->error();
    }

    /**
     * 接收excel添加记录
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PostMapping('import'), ChPermission('ch:attract:import'), ChOperationLog]
    public function importSaveRecord(ActiveAddFriendRecordRequest $request)
    {
        $uploadedFiles = $request->getUploadedFiles();
        if (empty($uploadedFiles) || !isset($uploadedFiles['excel_file'])) {
            throw new NormalStatusException("未上传Excel文件");
        }
        $file = $uploadedFiles['excel_file'];
        $params = $this->request->all();
        return $this->recordService->importSaveRecord($file, $params) ? $this->success() : $this->error();
    }

    /**
     * 更改标签
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PutMapping('setRecordTags'), ChPermission('ch:attract:add:setRecordTags'), ChOperationLog]
    public function setTags()
    {
        return $this->recordService->setTags($this->request->all()) ? $this->success() : $this->error();
    }

    /**
     * 删除标签
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[DeleteMapping('deleteRecord'), ChPermission('ch:attract:add:deleteRecord'), ChOperationLog]
    public function deleteRecord()
    {
        return $this->recordService->deleteRecord($this->request->all()) ? $this->success() : $this->error();
    }

    /**
     * 导出记录
     * @return ResponseInterface
     */
    #[GetMapping('exportRecord'), ChPermission('ch:attract:add:exportRecord'), ChOperationLog]
    public function recordExport()
    {
        return $this->recordService->exportRecord($this->request->all(), AddFriendRecordDto::class);
    }

    /**
     * 获取打招呼机器人列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('robotList'), ChPermission("ch:attract:add:robotList")]
    public function robotList(): ResponseInterface
    {
        return $this->success($this->robotService->robotList($this->request->all()));
    }

    /**
     * 添加打招呼企微号
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('addRobot'), ChPermission("ch:attract:add:addRobot"), ChOperationLog]
    public function addRobot()
    {
        $this->robotService->getNewRobot($this->request->all());
        return $this->success();
    }

    /**
     * 删除企微号
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[DeleteMapping('deleteRobot'), ChPermission("ch:attract:add:deleteRobot"), ChOperationLog]
    public function deleteRobot()
    {
        return $this->robotService->deleteRobot($this->request->input('id'))? $this->success():$this->error();
    }

    /**
     * 单个或批量更改企微号的状态,分组,打招呼上限
     * @param ActiveAddFriendRobotRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('setWechat'), ChPermission("ch:attract:add:setWechat"), ChOperationLog]
    public function setWechat(ActiveAddFriendRobotRequest $request): ResponseInterface
    {
        return $this->robotService->setWechat($request->all()) ? $this->success() : $this->error();
    }


    #[PostMapping('delWechat'), ChPermission("ch:attract:add:delWechat"), ChOperationLog]
    public function delWechat(ActiveAddFriendRobotRequest $request): ResponseInterface
    {
        return $this->robotService->delWechat($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 获取企微号日志
     * @param ActiveAddFriendRobotRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('wechatLog'), ChPermission("ch:attract:add:wechatLog")]
    public function getRobotLog(ActiveAddFriendRobotRequest $request): ResponseInterface
    {
        return $this->success($this->robotService->getRobotLog($request->all()));
    }

    /**
     * 分日数据
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('dailyData'), ChPermission("ch:attract:add:dailyData")]
    public function dailyData()
    {
        return $this->success($this->recordService->dailyData($this->request->all()));
    }

    /**
     * 导出分日数据
     * @return ResponseInterface
     */
    #[GetMapping('exportDailyData'), ChPermission("ch:attract:add:dailyData")]
    public function exportDailyData()
    {
        return $this->recordService->exportDailyData($this->request->all(), AddFriendDailyDataDto::class);
    }

    /**
     * 分号数据
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('robotData'), ChPermission("ch:attract:add:robotData")]
    public function robotData(): ResponseInterface
    {
        return $this->success($this->robotService->robotData($this->request->all()));
    }

    /**
     * 导出分号数据
     * @return ResponseInterface
     */
    #[GetMapping('exportRobotData'), ChPermission("ch:attract:add:dailyData")]
    public function exportRobotData(): ResponseInterface
    {
        return $this->robotService->exportRobotData($this->request->all(), AddFriendRobotDataDto::class);
    }

    /**
     * 分组数据
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('groupData'), ChPermission("ch:attract:add:groupData")]
    public function groupData(): ResponseInterface
    {
        return $this->success($this->groupService->groupData($this->request->all()));
    }

    /**
     * 导出分号数据
     * @return ResponseInterface
     */
    #[GetMapping('exportGroupData'), ChPermission("ch:attract:add:dailyData")]
    public function exportGroupData(): ResponseInterface
    {
        return $this->groupService->exportGroupData($this->request->all(), AddFriendGroupDataDto::class);
    }

    /**
     * 获取拉人群列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PostMapping('roomList'), ChPermission("ch:attract:add:roomList")]
    public function getRoom(): ResponseInterface
    {
        return $this->success($this->roomService->roomList($this->request->all()));
    }

    /**
     * 更改拉人群排序
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[PostMapping('changeRoomSort'), ChPermission("ch:attract:add:changeRoomSort")]
    public function changeRoomSort(): ResponseInterface
    {
        $this->roomService->changeRoomSort($this->request->all());
        return $this->success();
    }

    /**
     * 移除拉人群
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     */
    #[DeleteMapping('deleteRoom'), ChPermission("ch:attract:add:deleteRoom")]
    public function deleteRoom(): ResponseInterface
    {
        return $this->roomService->deleteRoom($this->request->all())? $this->success() : $this->error();
    }
}